﻿<UserControl
	x:Class="Sidvall.LogManager.UI.Wpf.Controls.ButtonControl" 
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
	xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
	xmlns:local="clr-namespace:Sidvall.LogManager.UI.Wpf.Controls" 
	xmlns:Converters="clr-namespace:Sidvall.UI.Wpf.Converters;assembly=Sidvall.UI.Wpf" 
	mc:Ignorable="d" 
	d:DesignHeight="50" d:DesignWidth="200">

	<UserControl.Resources>
		<Converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
		<Converters:BooleanToVisibilityConverter x:Key="NotBooleanToVisibilityConverter" Not="True"/>
		<Style x:Key="ButtonStyle" TargetType="{x:Type Button}" BasedOn="{x:Null}">
			<Setter Property="FocusVisualStyle" Value="{DynamicResource SimpleButtonFocusVisual}"/>
			<Setter Property="Background" Value="{DynamicResource NormalBrush}"/>
			<Setter Property="BorderBrush" Value="{DynamicResource NormalBorderBrush}"/>
			<Setter Property="Template">
				<Setter.Value>
					<ControlTemplate TargetType="{x:Type Button}">
						<!-- We use Grid as a root because it is easy to add more elements to customize the button -->
						<Grid x:Name="Grid">
							<Border x:Name="Border" Background="{TemplateBinding Background}" 
								BorderBrush="{TemplateBinding BorderBrush}" 
								BorderThickness="{TemplateBinding BorderThickness}" 
								Padding="{TemplateBinding Padding}"/>
							<!-- Content Presenter is where the text content etc is placed by the control -->
							<!-- The bindings are useful so that the control can be parameterized without editing the template -->
							<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
								Margin="{TemplateBinding Padding}" 
								VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
								RecognizesAccessKey="True"/>
						</Grid>
						<!--Each state sets a brush on the Border in the template -->
						<ControlTemplate.Triggers>
							<Trigger Property="IsKeyboardFocused" Value="true">
								<Setter Property="BorderBrush" Value="{DynamicResource DefaultedBorderBrush}" TargetName="Border"/>
							</Trigger>
							<Trigger Property="IsMouseOver" Value="true">
								<Setter Property="Background" Value="{Binding ButtonMouseOverBackground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ButtonControl}}}" TargetName="Border"/>
							</Trigger>
							<Trigger Property="IsPressed" Value="true">
								<Setter Property="Background" Value="{DynamicResource PressedBrush}" TargetName="Border"/>
								<Setter Property="BorderBrush" Value="{DynamicResource PressedBorderBrush}" TargetName="Border"/>
							</Trigger>
							<Trigger Property="IsEnabled" Value="true"/>
							<Trigger Property="IsEnabled" Value="false">
								<Setter Property="Background" Value="{Binding Background, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ButtonControl}}}" TargetName="Border"/>
								<Setter Property="BorderBrush" Value="{Binding BorderBrush, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ButtonControl}}}" TargetName="Border"/>
								<Setter Property="Foreground" Value="{DynamicResource DisabledForegroundBrush}"/>
								<Setter Property="Opacity" Value="0.5"/>
							</Trigger>
						</ControlTemplate.Triggers>
					</ControlTemplate>
				</Setter.Value>
			</Setter>
		</Style>
	</UserControl.Resources>

	<Button x:Name="Button" 
			Background="{Binding Background, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ButtonControl}}}"
			BorderBrush="{Binding BorderBrush, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ButtonControl}}}"
			BorderThickness="0" Style="{DynamicResource ButtonStyle}"
			Command="{Binding ButtonCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ButtonControl}}}" 
			CommandParameter="{Binding ButtonCommandParameter, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ButtonControl}}}"
			VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch">
		<Grid >
			<Grid.ColumnDefinitions>
				<ColumnDefinition Width="Auto" />
				<ColumnDefinition />
			</Grid.ColumnDefinitions>
			<Image Margin="{Binding ImageMargin, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ButtonControl}}}" 
					Source="{Binding ImageSource, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ButtonControl}}}" 
					Height="{Binding ImageHeight, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ButtonControl}}}" 
					Width="{Binding ImageWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ButtonControl}}}" 
					Visibility="{Binding IsEnabled, Converter={StaticResource BooleanToVisibilityConverter}, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ButtonControl}}}">
				<Image.Effect>
					<DropShadowEffect/>
				</Image.Effect>
			</Image>
			<Image Margin="{Binding ImageMargin, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ButtonControl}}}" 
				    Source="{Binding ImageSource, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ButtonControl}}}"
					Height="{Binding ImageHeight, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ButtonControl}}}" 
					Width="{Binding ImageWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ButtonControl}}}" 
					Visibility="{Binding IsEnabled, Converter={StaticResource NotBooleanToVisibilityConverter}, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ButtonControl}}}">
			</Image>
			<TextBlock Grid.Column="1" Text="{Binding ButtonText, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ButtonControl}}}" VerticalAlignment="Center" FontSize="14" Margin="5,0,0,0" />
		</Grid>
	</Button>
</UserControl>
